﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace MvcPractice.Controllers
{
    public class IndexController : Controller
    {
        ZuoYeEntities zy = new ZuoYeEntities();
        // GET: Index
        public ActionResult Index()
        {
            var a = zy.Students.ToList();

            return View(a);
        }

        // GET: Index/Details/5
        public ActionResult Details(int id)
        {
          
            var ol = zy.Students.Where(a => a.StuID == id).FirstOrDefault();
            zy.SaveChanges();
            return View(ol);
        }

        // GET: Index/Create
        public ActionResult Create(Students stu)
        {
            //绑定droplist数据
            ViewBag.Categories = new SelectList(zy.Class, "ClassID", "ClassName");

            return View();
        }

        // POST: Index/Create
        [HttpPost]
        public ActionResult Create(FormCollection collection,Students su)
        {
            try
            {
                // TODO: Add insert logic here
                zy.Students.Add(su);
                zy.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Index/Edit/5
        public ActionResult Edit(int id)
        {
            var b = zy.Students.Where(a => a.StuID == id).FirstOrDefault() ;
            ViewData.Model = b;
            return View();
        }

        // POST: Index/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection,Students ss)
        {
            try
            {
                // TODO: Add update logic here
                zy.Students.Attach(ss);
                zy.Entry(ss).State = EntityState.Modified;
                zy.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Index/Delete/5
        public ActionResult Delete(int id)
        {
            var c = zy.Students.Where(a => a.StuID == id).FirstOrDefault();
            ViewData.Model=c;
            return View();
        }

        // POST: Index/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                var c = zy.Students.Where(a => a.StuID == id).FirstOrDefault();
                zy.Students.Remove(c);
                zy.SaveChanges();
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
